package data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class FixedPackage extends AbstractData{
	private long id = -1;
	private  long hotelId;
	private String packageName;
	private long longOfStay;
	private boolean	isForever;
	private Date from;
	private Date to;
	private String description;
	private GuestType availableFor;
	private String availableForString;
	private List<PackagePrice> packagepriceList;
	private String packagepriceListString;
	private boolean status;
	private boolean avaliable;
	private boolean isArchive;
	private Date date;

	public long getId() {
		return id;
	}

	public long getHotelId() {
		return hotelId;
	}

	public void setHotelId(long hotelId) {
		this.hotelId = hotelId;
	}

	
	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public long getLongOfStay() {
		return longOfStay;
	}

	public void setLongOfStay(long longOfStay) {
		this.longOfStay = longOfStay;
	}

	public boolean isForever() {
		return isForever;
	}

	public void setForever(boolean isForever) {
		this.isForever = isForever;
	}

	public Date getFrom() {
		return from;
	}

	public void setFrom(Date from) {
		this.from = from;
	}

	public Date getTo() {
		return to;
	}

	public void setTo(Date to) {
		this.to = to;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public GuestType getAvailableFor() {
		if (availableFor == null && !(availableForString == null || availableForString.isEmpty())) {
			GuestType g = new GuestType(Long.parseLong(availableForString));
			g.LoadData();
			availableFor = g;
		}
		return availableFor;
	}

	public void setAvailableFor(GuestType availableFor) {
		this.availableFor = availableFor;
		this.availableForString = String.format("%d", availableFor.getId());
	}

	public String getAvailableForString() {
		return availableForString;
	}

	public void setAvailableForString(String availableForString) {
		this.availableForString = availableForString;
	}

	public String getPackagepriceListString() {
		return packagepriceListString;
	}

	public void setId(long id) {
		this.id = id;
	}

	public List<PackagePrice> getPackagepriceList() {
		if ((packagepriceList == null || packagepriceList.size() == 0) && !(packagepriceListString == null || packagepriceListString.isEmpty())) {
			String[] idOfList = SplitList(packagepriceListString);
			List<PackagePrice> list = new ArrayList<PackagePrice>();
			for (String id : idOfList) {
				PackagePrice a = new PackagePrice(Long.parseLong(id));
				list.add(a);
			}
			for(PackagePrice a : list)
				a.LoadData();
			packagepriceList = list;
		}
		return packagepriceList;
	}

	public void setPackagepriceList(List<PackagePrice> packagepriceList) {
		this.packagepriceListString = CombineList(packagepriceList);
		this.packagepriceList = packagepriceList;
	}

	
	public void setPackagepriceListString(String packagepriceListString) {
		this.packagepriceListString = packagepriceListString;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public boolean isAvaliable() {
		return avaliable;
	}

	public void setAvaliable(boolean avaliable) {
		this.avaliable = avaliable;
	}
	
	public boolean isArchive() {
		return isArchive;
	}

	public void setArchive(boolean isArchive) {
		this.isArchive = isArchive;
	}
	
	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}
	
	@Override
	public Entity getEntity() {
		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.FixedPackage);
		else
			item = new Entity(this.key);
		item.setProperty("hotelId", this.hotelId);
		item.setProperty("packageName", this.packageName);
		item.setProperty("longOfStay", this.longOfStay);
		item.setProperty("packagepriceList", this.packagepriceListString);
		item.setProperty("status", this.status);
		item.setProperty("avaliable", this.avaliable);
		item.setProperty("availableForString", this.availableForString);
		item.setProperty("description", this.description);
		item.setProperty("to", this.to);
		item.setProperty("from", this.from);
		item.setProperty("isForever", this.isForever);
		item.setProperty("isArchive", this.isArchive);
		item.setProperty("date", this.date);
		return item;
	}
	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
		this.hotelId = (Long) entity.getProperty("hotelId");
		this.packageName = (String) entity.getProperty("packageName");
		this.packagepriceListString = (String) entity.getProperty("packagepriceList");
		this.status = (Boolean) entity.getProperty("status");
		this.avaliable = (Boolean) entity.getProperty("avaliable");
		this.longOfStay = (Long) entity.getProperty("longOfStay");
		this.availableForString = (String) entity.getProperty("availableForString");
		this.description = (String) entity.getProperty("description");
		this.to = (Date) entity.getProperty("to");
		this.from = (Date) entity.getProperty("from");
		this.isForever = (Boolean) entity.getProperty("isForever");
		this.isArchive = (Boolean) entity.getProperty("isArchive");
		this.date = (Date) entity.getProperty("date");
		this.key = entity.getKey();
	}
	
	public void LoadData() {
		if (this.id > -1) {
			Entity entity = LoadData(EntityName.FixedPackage, this.id);
			setEntity(entity);
		}
	}
}
